<template>
  <a-spin :spinning="confirmLoading">
    <j-form-container :disabled="formDisabled">
      <a-form :form="form" slot="detail">
        <a-row>
          <a-col :span="24" v-if="flage2">
            <a-form-item label="缺陷内容" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <a-input v-decorator="['troubleDetail', validatorRules.troubleDetail]" placeholder="请输入缺陷内容"></a-input>
            </a-form-item>
          </a-col>
          <a-col :span="24" v-if="flage2">
            <a-form-item label="是否处理" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <!-- <j-dict-select-tag type="list" v-decorator="['sFlag', validatorRules.sFlag,{initialValue:'未处理'}]" :trigger-change="true" dictCode="faults_status" placeholder="请选择是否处理"/> -->
               <j-search-select-tag type="list" v-decorator="['sFlag', validatorRules.sFlag,{initialValue:'未处理'}]" :trigger-change="true" dictCode="" placeholder="请选择是否处理"  :dictOptions="dictOption"/>
            </a-form-item>
          </a-col>
          <a-col :span="24" v-if="flage2">
            <a-form-item label="发现人" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <a-input v-decorator="['subName', validatorRules.subName]" placeholder="请输入巡检人名称"></a-input>
              <!-- <j-search-select-tag  placeholder="请选择巡检人名称" v-decorator="['subName', validatorRules.subName]" :trigger-change="true" dictCode="" type="list" setFieldsValue="selectValue" :dictOptions="dictOptions"> </j-search-select-tag> -->
            </a-form-item>
          </a-col>
          <a-col :span="24" v-if="flage2">
            <a-form-item label="发现时间" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <j-date placeholder="请选择发现时间" v-decorator="['findDate', validatorRules.findDate]" :trigger-change="true" :show-time="true" date-format="YYYY-MM-DD" style="width: 100%"/>
            </a-form-item>
          </a-col>
          <a-col :span="24" v-if="flage2">
              <a-form-item label="问题等级" :labelCol="labelCol" :wrapperCol="wrapperCol">
                  <j-search-select-tag  placeholder="请输入问题等级" v-decorator="['troubleLevel']" :trigger-change="true" setFieldsValue="selectValue" :dictOptions="dict"> </j-search-select-tag>
              </a-form-item>
            </a-col>
             <a-col :span="24" v-if="flage2">
            <a-form-item label="建议解决方案" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <a-input v-decorator="['adviceDetail']" placeholder="请输入处理明细"></a-input>
            </a-form-item>
          </a-col>
          <a-col :span="24" v-if="flage1">
            <a-form-item label="处理人" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <a-input  placeholder="请选择处理人名称" v-decorator="['solveName', validatorRules.solveName]" :trigger-change="true" dictCode="" type="list"> </a-input>
            </a-form-item>
          </a-col>
          <a-col :span="24" v-if="flage1" >
            <a-form-item label="处理时间" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <j-date placeholder="请选择处理时间" v-decorator="['solveDate',validatorRules.solveDate]" :trigger-change="true" :show-time="true" date-format="YYYY-MM-DD" style="width: 100%"/>
            </a-form-item>
          </a-col>
          <a-col :span="24" v-if="flage1" >
            <a-form-item label="处理明细" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <a-input v-decorator="['solveDetail',validatorRules.solveDetail]" placeholder="请输入处理明细"></a-input>
            </a-form-item>
          </a-col>
          <a-col :span="24" v-if="flage2">
            <a-form-item label="部门名称" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <!-- <j-select-depart v-decorator="['orgName', validatorRules.orgName,{initialValue:queryParam.orgCode}]"/> -->
              <j-select-depart v-decorator="['orgName', validatorRules.orgName,{initialValue:queryParam.orgCode}]" id="orgname"/>
            </a-form-item>
          </a-col>
          <a-col v-if="showFlowSubmitButton" :span="24" style="text-align: center">
            <a-button @click="submitForm">提 交</a-button>
          </a-col>
        </a-row>
      </a-form>
    </j-form-container>
  </a-spin>
</template>

<script>

  import { httpAction, getAction } from '@/api/manage'
  import pick from 'lodash.pick'
  import { validateDuplicateValue } from '@/utils/util'
  import JFormContainer from '@/components/jeecg/JFormContainer'
  import JDate from '@/components/jeecg/JDate'  
  import JSelectDepart from '@/components/jeecgbiz/JSelectDepart'
  import JDictSelectTag from "@/components/dict/JDictSelectTag"
  import JSearchSelectTag from '@/components/dict/JSearchSelectTag'
  import store from '@/store'

  export default {
    name: 'DtFaultsForm',
    components: {
      JFormContainer,
      JDate,
      JSelectDepart,
      JDictSelectTag,
      JSearchSelectTag
    },
    props: {
      //流程表单data
      formData: {
        type: Object,
        default: ()=>{},
        required: false
      },
      //表单模式：true流程表单 false普通表单
      formBpm: {
        type: Boolean,
        default: false,
        required: false
      },
      //表单禁用
      disabled: {
        type: Boolean,
        default: false,
        required: false
      }
    },
    data () {
      return {
         queryParam:{
          orgCode:''
        },
        flage1:false,
        flage2:true,
        selectValue:"",
        dictOptions:[{
          text:"选项一",
          value:"1"
        },{
          text:"选项二",
          value:"2"
        },{
          text:"选项三",
          value:"3"
        }],
        dictOption:[{
          text:"未处理",
          value:'0'
        }],
          dict:[{
          text:"简单",
          value:"1"
        },{
          text:"中等",
          value:"2"
        },{
          text:"复杂",
          value:"3"
        }],
        form: this.$form.createForm(this),
        model: {},
        labelCol: {
          xs: { span: 24 },
          sm: { span: 5 },
        },
        wrapperCol: {
          xs: { span: 24 },
          sm: { span: 16 },
        },
        confirmLoading: false,
        validatorRules: {
          troubleDetail: {
            rules: [
              { required: true, message: '请输入缺陷内容!'},
            ]
          },
          sFlag: {
            rules: [
              { required: true, message: '请输入是否处理!'},
            ]
          },
          subName: {
            rules: [
              { required: true, message: '请输入发现人!'},
            ]
          },
          findDate: {
            rules: [
              { required: true, message: '请输入发现时间!'},
            ]
          },
          orgName: {
            rules: [
              { required: true, message: '请输入部门名称!'},
            ]
          },
          solveName:{
            rules:[
              {required:true,message:'请选择处理人'}
            ]
          },
          solveDate:{
            rules:[
              {required:true,message:'请输入处理时间'}
            ]
          },
          solveDetail:{
            rules:[
              {required:true,message:'请选择处理明细'}
            ]
          },
        },
        url: {
          add: "/trouble_storage/troubleStorage/add?sFlag=0",
          edit: "/trouble_storage/troubleStorage/solve",
          queryById: "/dtfault/dtFaults/queryById",
          Approverlist:"/dtapprover/dtApprover/list",
        }
      }
    },
    computed: {
      formDisabled(){
        if(this.formBpm===true){
          if(this.formData.disabled===false){
            return false
          }
          return true
        }
        return this.disabled
      },
      showFlowSubmitButton(){
        if(this.formBpm===true){
          if(this.formData.disabled===false){
            return true
          }
        }
        return false
      }
    },
    created () {
      //如果是流程中表单，则需要加载流程表单data
      this.showFlowData();
    },
    methods: {
      add () {
        var obj={}
      obj.orgName = store.getters.userInfo.departIds
      this.$nextTick(() => {
        this.form.setFieldsValue(pick(obj,'orgName'))  //使添加的时候所属风场是登录人所在的风场
      })
        this.edit({});
      },
      edit (record) {
        console.log('record',record)
         if(record.id!=null){
          //编辑
          this.flage1 = true
          this.flage2 = false
        }else{
          this.flage1 = false
          this.flage2 = true
        }
        this.form.resetFields();
        this.model = Object.assign({}, record);
        this.visible = true;
        this.$nextTick(() => {
          this.form.setFieldsValue(pick(this.model,'troubleDetail','subName','findDate','solveName','solveDate','solveDetail','orgName','troubleLevel'))
        })
      },
      //渲染流程表单数据
      showFlowData(){
        if(this.formBpm === true){
          let params = {id:this.formData.dataId};
          getAction(this.url.queryById,params).then((res)=>{
            if(res.success){
              this.edit (res.result);
            }
          });
        }
      },
      submitForm () {
        const that = this;
        // 触发表单验证
        this.form.validateFields((err, values) => {
          if (!err) {
            that.confirmLoading = true;
            let httpurl = '';
            let method = '';
            if(!this.model.id){
              httpurl+=this.url.add;
              this.flage1 = false
              method = 'post';
            }else{
              httpurl+=this.url.edit;
              this.flage1 = false
               method = 'put';
            }
            let formData = Object.assign(this.model, values);
            console.log("表单提交数据",formData)
            httpAction(httpurl,formData,method).then((res)=>{
              if(res.success){
                that.$message.success(res.message);
                that.$emit('ok');
              }else{
                that.$message.warning(res.message);
              }
            }).finally(() => {
              that.confirmLoading = false;
            })
          }
         
        })
      },
      popupCallback(row){
        this.form.setFieldsValue(pick(row,'troubleDetail','subName','findDate','solveName','solveDate','solveDetail','orgName','troubleLevel'))
      },
      getpeople(){
         // this.initDictConfig();
       var params = {};
    params.pageNo = 1;
    params.pageSize = 9999;
    getAction(this.url.Approverlist, params).then((res) => {
        var objlist = [];
        if (res.success) {
          for(var i=0;i<res.result.records.length;i++){
            var obj = {};
            obj.value = res.result.records[i].id;
            obj.text = res.result.records[i].approverName;
            obj.title = res.result.records[i].approverName;
            objlist.push(obj);
          }
          this.dictOptions = objlist;
        }
        if(res.code===510){
          this.$message.warning(res.message)
        }
        
      })
      }
    },
    mounted(){
      this.queryParam.personName =  store.getters.userInfo.username
      this.queryParam.orgCode = store.getters.userInfo.orgCode
      this.getpeople()
    }
  }
</script>